package com.example.demorsa.serivce;

/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import javax.annotation.PostConstruct;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.util.Base64;

import static org.apache.tomcat.util.net.openssl.ciphers.Authentication.RSA;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-09-27
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-09-27                      liyuncong          Created
 */
//@Service
public class RsaService {

    @PostConstruct
    private void generator() throws NoSuchAlgorithmException {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA.toString());
        generator.initialize(2048);
        final KeyPair key = generator.generateKeyPair();

        if (key.getPrivate() instanceof RSAPrivateKey) {
            System.out.println(true);
        }

        System.out.println("public :  ++++++");
        final String publicKey =
            Base64.getMimeEncoder().encodeToString(key.getPublic().getEncoded());


        System.out.println("ssh-rsa " + publicKey + " zhangsan@qq.com");
        System.out.println("private :  ++++++");
        final String privateKey =
            Base64.getMimeEncoder().encodeToString(key.getPrivate().getEncoded());
        final String encodedPrivateKey = "-----BEGIN " + RSA + " PRIVATE KEY-----\n"
            + privateKey + "\n"
            + "-----END " + RSA + " PRIVATE KEY-----";
        System.out.println(encodedPrivateKey);
    }
}
